package cn.it.ygh.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import cn.it.ygh.pojo.Agent;
import cn.it.ygh.service.AgentService;

@Controller(value="AgentController")
public class AgentController {
	@Autowired
	private AgentService agentService;
	
	@RequestMapping(value="/agentLogin.do",method=RequestMethod.POST)
	public ModelAndView login(Agent ag,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		
		Agent agent = agentService.login(ag);
		System.out.println(ag.getAgentAccount()+"-"+ag.getAgentPwd());
		if (agent == null) {
			modelAndView.addObject("error", "登入失败！");
			System.out.println("登录失败");
			modelAndView.setViewName("../agents/jsp/AgentLogin");
			return modelAndView;
		}else{
			modelAndView.addObject("agent", agent);
			modelAndView.setViewName("../agents/jsp/AgentInfo");
			session.setAttribute("ag", agent);
			return modelAndView;
		}
	}
	
	
	
	@RequestMapping(value="/agentRegister.do",method=RequestMethod.POST)
	public ModelAndView register(Agent ag) {
		ModelAndView modelAndView = new ModelAndView();
		
		Agent agent = agentService.register(ag);
		if (agent == null) {
			modelAndView.addObject("error", "注册失败！");
			modelAndView.setViewName("../agents/jsp/AgentRegister");
			return modelAndView;
		}
		modelAndView.addObject("agent", agent);
		modelAndView.setViewName("../agents/jsp/WaitingReview");
		return modelAndView;
		
	}
	
	
	@RequestMapping(value="/agentModify.do",method=RequestMethod.POST)
	public ModelAndView update(Agent ag) {
		ModelAndView modelAndView = new ModelAndView();
		agentService.update(ag);
		System.out.println(ag.getAgentAccount());
		modelAndView.setViewName("../agents/jsp/AgentLogin");
		return modelAndView;
		
	}
	
	@RequestMapping(value="/agentImg.do",method=RequestMethod.POST)
	public ModelAndView updateImg(Agent ag,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		Agent agent = (Agent) session.getAttribute("ag");
		agent.setAgentImg(ag.getAgentImg());
		session.setAttribute("ag", agent);
		ag.setAgentAccount(agent.getAgentAccount());
		agentService.updateImg(ag);
		
		modelAndView.setViewName("../agents/jsp/ModifyInfo");
		return modelAndView;
		
	}
	
	
	@RequestMapping(value="/ModifyagentImg.do",method=RequestMethod.POST)
	public ModelAndView updateModifyImg(Agent ag) {
		ModelAndView modelAndView = new ModelAndView();
		agentService.updateImg(ag);
		
		modelAndView.setViewName("../agents/jsp/ModifyImg");
		return modelAndView;
		
	}
	
	@RequestMapping(value="/ifagentLogin.do")
	public ModelAndView ifagentLogin(Agent ag,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		if(session.getAttribute("ag") != null){
			modelAndView.setViewName("../agents/jsp/AgentInfo");
		}
		else{
			modelAndView.setViewName("../agents/jsp/AgentLogin");
		}
		return modelAndView;
		
	}
	
	
}
